package net.wesc.ac.server;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpSession;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.wesc.ac.client.MainPanel;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;

public class FitPlotServlet extends HttpServlet{

	/**
	 * 
	 */
	private static final long serialVersionUID = 4993680464858090895L;

	@Override
	public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
		HttpSession session =request.getSession();
		double[] param = (double[])session.getAttribute("param");
		String data = (String)session.getAttribute("data");
		String model = (String)session.getAttribute("model");
		String type = (String)session.getAttribute("type");
		String temp = (String)session.getAttribute("temp");
		if(data==null || param==null || model==null || type==null || temp==null){
			return;
		}
		else{
			session.removeAttribute("data");
			session.removeAttribute("param");
			session.removeAttribute("model");
			session.removeAttribute("type");
			session.removeAttribute("temp");
		}
		
		double[][] expdata = Data.getData(data);
		try{
			JFreeChart chart = createChart(expdata, param, model, type, temp);
			response.setContentType("image/png");
			ChartUtilities.writeChartAsPNG(response.getOutputStream(), chart, 600, 520);
		}
		finally{

		}
	}
	
	@Override
	public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
		doGet(request, response);
	}
	
	private JFreeChart createChart(double[][] data, double[] param, String model, String type, String temp){
		double[] interval = new double[2];
		interval[0] = data[0][0];//第一组实验数据的x
		interval[1] = data[data.length-1][0];//最后一组实验数据的x
    	XYDataset dataset = createDataset(data, data.length, interval, param, model, type, temp);
    	JFreeChart chart = ChartFactory.createXYLineChart("Relation Between ln(r) and molality", "molality mol/kg(H2O)", "log(r)", dataset, PlotOrientation.VERTICAL, false, false, true);
    	return chart;
	}
    
    private XYDataset createDataset(double[][] data, int samples, double[] interval, double[] param, String model, String type, String ttemp) {
		XYSeries xyseries1 = new XYSeries("Fit");
		int i=0;
		double temp = Double.parseDouble(ttemp);
		while(i<data.length){
			if(model.matches(MainPanel.DH_EQUATION)){
				if(type.matches(MainPanel.E11_TYPE)){
					xyseries1.add(data[i][0], Model.DH(1,1,data[i][0], param,temp));
					i++;
				}
				if(type.matches(MainPanel.E12_TYPE)){
					xyseries1.add(data[i][0], Model.DH(1,2,data[i][0], param,temp));
					i++;
				}
				if(type.matches(MainPanel.E13_TYPE)){
					xyseries1.add(data[i][0], Model.DH(1,3,data[i][0], param,temp));
					i++;
				}
				if(type.matches(MainPanel.E21_TYPE)){
					xyseries1.add(data[i][0], Model.DH(2,1,data[i][0], param,temp));
					i++;
				}
				if(type.matches(MainPanel.E22_TYPE)){
					xyseries1.add(data[i][0], Model.DH(2,2,data[i][0], param,temp));
					i++;
				}
				if(type.matches(MainPanel.E23_TYPE)){
					xyseries1.add(data[i][0], Model.DH(2,3,data[i][0], param,temp));
					i++;
				}
				if(type.matches(MainPanel.E31_TYPE)){
					xyseries1.add(data[i][0], Model.DH(3,1,data[i][0], param,temp));
					i++;
				}
				if(type.matches(MainPanel.E32_TYPE)){
					xyseries1.add(data[i][0], Model.DH(3,2,data[i][0], param,temp));
					i++;
				}
				if(type.matches(MainPanel.E33_TYPE)){
					xyseries1.add(data[i][0], Model.DH(3,3,data[i][0], param,temp));
					i++;
				}
			}
			
			if(model.matches(MainPanel.HE_EQUATION)){
				if(type.matches(MainPanel.E11_TYPE)){
					xyseries1.add(data[i][0], Model.HE(1, 1, data[i][0], param, temp));
					i++;
				}
				if(type.matches(MainPanel.E12_TYPE)){
					xyseries1.add(data[i][0], Model.HE(1, 2, data[i][0], param, temp));
					i++;
				}
				if(type.matches(MainPanel.E13_TYPE)){
					xyseries1.add(data[i][0], Model.HE(1, 3, data[i][0], param, temp));
					i++;
				}
				if(type.matches(MainPanel.E21_TYPE)){
					xyseries1.add(data[i][0], Model.HE(2, 1, data[i][0], param, temp));
					i++;
				}
				if(type.matches(MainPanel.E22_TYPE)){
					xyseries1.add(data[i][0], Model.HE(2, 2, data[i][0], param, temp));
					i++;
				}
				if(type.matches(MainPanel.E23_TYPE)){
					xyseries1.add(data[i][0], Model.HE(2, 3, data[i][0], param, temp));
					i++;
				}
				if(type.matches(MainPanel.E31_TYPE)){
					xyseries1.add(data[i][0], Model.HE(3, 1, data[i][0], param, temp));
					i++;
				}
				if(type.matches(MainPanel.E32_TYPE)){
					xyseries1.add(data[i][0], Model.HE(3, 2, data[i][0], param, temp));
					i++;
				}
				if(type.matches(MainPanel.E33_TYPE)){
					xyseries1.add(data[i][0], Model.HE(3, 3, data[i][0], param, temp));
					i++;
				}
			}
			
			if(model.matches(MainPanel.RS_EQUATION)){
				if(type.matches(MainPanel.E11_TYPE)){
					xyseries1.add(data[i][0], Model.RS(1, 1, data[i][0], data[i][1], param, temp));
					i++;
				}
				if(type.matches(MainPanel.E12_TYPE)){
					xyseries1.add(data[i][0], Model.RS(1, 2, data[i][0], data[i][1], param, temp));
					i++;
				}
				if(type.matches(MainPanel.E13_TYPE)){
					xyseries1.add(data[i][0], Model.RS(1, 3, data[i][0], data[i][1], param, temp));
					i++;
				}
				if(type.matches(MainPanel.E21_TYPE)){
					xyseries1.add(data[i][0], Model.RS(2, 1, data[i][0], data[i][1], param, temp));
					i++;
				}
				if(type.matches(MainPanel.E22_TYPE)){
					xyseries1.add(data[i][0], Model.RS(2, 2, data[i][0], data[i][1], param, temp));
					i++;
				}
				if(type.matches(MainPanel.E23_TYPE)){
					xyseries1.add(data[i][0], Model.RS(2, 3, data[i][0], data[i][1], param, temp));
					i++;
				}
				if(type.matches(MainPanel.E31_TYPE)){
					xyseries1.add(data[i][0], Model.RS(3, 1, data[i][0], data[i][1], param, temp));
					i++;
				}
				if(type.matches(MainPanel.E32_TYPE)){
					xyseries1.add(data[i][0], Model.RS(3, 2, data[i][0], data[i][1], param, temp));
					i++;
				}
				if(type.matches(MainPanel.E33_TYPE)){
					xyseries1.add(data[i][0], Model.RS(3, 3, data[i][0], data[i][1], param, temp));
					i++;
				}
			}
			
			if(model.matches(MainPanel.GS_EQUATION)){
				if(type.matches(MainPanel.E11_TYPE)){
					xyseries1.add(data[i][0], Model.GS(1, 1, data[i][0], param, temp));
					i++;
				}
				if(type.matches(MainPanel.E12_TYPE)){
					xyseries1.add(data[i][0], Model.GS(1, 2, data[i][0], param, temp));
					i++;
				}
				if(type.matches(MainPanel.E13_TYPE)){
					xyseries1.add(data[i][0], Model.GS(1, 3, data[i][0], param, temp));
					i++;
				}
				if(type.matches(MainPanel.E21_TYPE)){
					xyseries1.add(data[i][0], Model.GS(2, 1, data[i][0], param, temp));
					i++;
				}
				if(type.matches(MainPanel.E22_TYPE)){
					xyseries1.add(data[i][0], Model.GS(2, 2, data[i][0], param, temp));
					i++;
				}
				if(type.matches(MainPanel.E23_TYPE)){
					xyseries1.add(data[i][0], Model.GS(2, 3, data[i][0], param, temp));
					i++;
				}
				if(type.matches(MainPanel.E31_TYPE)){
					xyseries1.add(data[i][0], Model.GS(3, 1, data[i][0], param, temp));
					i++;
				}
				if(type.matches(MainPanel.E32_TYPE)){
					xyseries1.add(data[i][0], Model.GS(3, 2, data[i][0], param, temp));
					i++;
				}
				if(type.matches(MainPanel.E33_TYPE)){
					xyseries1.add(data[i][0], Model.GS(3, 3, data[i][0], param, temp));
					i++;
				}
			}
			
			if(model.matches(MainPanel.PP_EQUATION)){
				if(type.matches(MainPanel.E11_TYPE)){
					xyseries1.add(data[i][0], Model.PP(1, 1, data[i][0], param, temp));
					i++;
				}
			}
		}
        
        XYSeriesCollection xySeriesCollection = new XYSeriesCollection();
        xySeriesCollection.addSeries(xyseries1);
        return xySeriesCollection;
    }
}